﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using RabbitDownload.WebSite.Local.TagManger;
using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Konachan.GetTag
{
    internal class GetTagScanner
    {
        public static List<string> Comblist = new List<string>() { "Any", "General", "Artist", "Copyright", "Character", "Style", "Circle" };

        public List<string> GetMode(String Mode, string ImgCountText, bool OpenFloderFlag = true)
        {
            int ImgCount = TextHelper.StringToInt(ImgCountText);
            switch (Mode)
            {
                case "Any":
                    return Start("http://konachan.net/tag.xml?name=&type=&order=count&limit=0", "K站全部标签", ImgCount, OpenFloderFlag);

                case "General":
                    return Start("http://konachan.net/tag.xml?name=&type=0&order=count&limit=0", "K站特征标签", ImgCount, OpenFloderFlag);

                case "Artist":
                    return Start("http://konachan.net/tag.xml?name=&type=1&order=count&limit=0", "K站创作者标签", ImgCount, OpenFloderFlag);

                case "Copyright":
                    return Start("http://konachan.net/tag.xml?name=&type=3&order=count&limit=0", "K站版权标签", ImgCount, OpenFloderFlag);

                case "Character":
                    return Start("http://konachan.net/tag.xml?name=&type=4&order=count&limit=0", "K站动漫角色标签", ImgCount, OpenFloderFlag);

                case "Style":
                    return Start("http://konachan.net/tag.xml?name=&type=5&order=count&limit=0", "K站风格样式标签", ImgCount, OpenFloderFlag);

                case "Circle":
                    return Start("http://konachan.net/tag.xml?name=&type=6&order=count&limit=0", "K站Circle标签", ImgCount, OpenFloderFlag);
            }
            return new List<string>();
        }

        /// <summary>
        /// 获取所有标签
        /// </summary>
        /// <param name="ImgCountText"></param>
        internal void GetAllTag(string ImgCountText)
        {
            List<string> AllTag = new List<string>();
            foreach (var item in Comblist)
            {
                Console.WriteLine(item);
                AllTag.AddRange(GetMode(item, ImgCountText, false));
            }
            String FilePath = StaticValueOld.SaveListPath + string.Format("KonachanAllTag-ImgCountGreater：{0}.txt", ImgCountText);
            FileHelper.WriteList(FilePath, AllTag);
        }

        /// <summary>
        /// 启动扫描标签
        /// </summary>
        /// <param name="XmlLink"></param>
        /// <param name="FileName"></param>
        /// <param name="ImgCount"></param>
        /// <param name="OpenFloderFlag"></param>
        /// <returns></returns>
        public List<string> Start(String XmlLink, string FileName, int ImgCount, bool OpenFloderFlag = true)
        {
            Console.WriteLine("开始获取{0}，请耐心等待。" + DateTime.Now, FileName);
            Console.WriteLine(XmlLink);
            String Shtml = WebHelper.HttpPcGet(XmlLink);
            Console.WriteLine("读取词库完成，正在处理数据。" + DateTime.Now);
            List<string> AllTag = GetTagList(Shtml, ImgCount);
            Console.WriteLine(AllTag.Count);
            String FilePath = StaticValueOld.SaveListPath + FileName + ".txt";
            if (FileHelper.FileExits(FilePath))
                FileHelper.FileDelete(FilePath);
            FileHelper.WriteList(FilePath, AllTag);
            if (OpenFloderFlag)
                FloderHelper.OpenFloder(StaticValueOld.SaveListPath);
            return AllTag;
        }

        private List<string> GetTagList(string shtml, int ImgCount)
        {
            List<string> Taglist = new List<string>();
            List<string> ValueList = HtmlHelper.GetHtmlValueList(shtml, "//tag");
            int CompltedCount = 0;
            foreach (var item in ValueList)
            {
                new PrintLog().PercentLog(ValueList.Count, CompltedCount++);
                string RomaTag = HtmlHelper.GetFirstAttributes(item, "name", "//tag").Replace("_", " ");
                int Count = TextHelper.StringToInt(HtmlHelper.GetFirstAttributes(item, "count", "//tag"));
                if (Count < ImgCount)
                    continue;
                Taglist.Add(string.Format(LocalTagStatcivalue.TagInfoMode, RomaTag, Count));
            }
            return Taglist;
        }
    }
}